package com.celesea.auth.dept.vo;

import com.celesea.auth.dept.entity.Dept;
import com.celesea.auth.dept.entity.DeptUser;
import com.celesea.auth.dept.v1.controller.request.PostRequest;
import com.celesea.framework.util.CollectionKit;
import com.celesea.framework.util.Const;
import com.celesea.framework.util.EnvUtil;
import com.celesea.framework.util.PinyinKit;
import com.celesea.framework.vo.DictionaryVo;
import org.springframework.beans.BeanUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * ${DESCRIPTION}
 *
 * @author JackYuan
 * @create 2017-10-29 22:25
 */
public class DeptVo extends Dept {

    private DictionaryVo admin;

    private DictionaryVo parent;

    private List<DictionaryVo> members;

    private List<DictionaryVo> invisibleMembers;

    private List<DictionaryVo> admins;

    public DeptVo() {
    }

    public DeptVo(PostRequest request) {
        BeanUtils.copyProperties(request, this);
        this.setPy(PinyinKit.getPinyinFirst(this.getName()));
        this.setParent(request.getParent());
        if (request.getParent() != null) {
            this.setParentId(request.getParent().getId());
            this.setParentName(request.getParent().getName());
        }
        if (request.getAdmin() != null) {
            this.setAdminId(request.getAdmin().getId());
            this.setAdminName(request.getAdmin().getName());
        }
    }

    public DeptVo(Dept dept) {
        BeanUtils.copyProperties(dept, this);
    }

    public Dept toEntity() {
        Dept entity = new Dept();
        BeanUtils.copyProperties(this, entity);
        if(Const.Bool.isFalse(entity.getCompanyFlag())){
            entity.setAdminId(null);
            entity.setAdminName(null);
        }
        return entity;
    }

    public Dept toUnique() {
        Dept entity = new Dept();
        entity.setId(this.id);
        entity.setCode(this.getCode());
        entity.setTenantId(EnvUtil.getTenantId());
        return entity;
    }


    public void fillDeptMembers(List<DeptUser> users) {
        if(CollectionKit.isEmpty(users)){
            return;
        }
        List<DeptUser> members = users.stream().filter(item ->
                Const.Bool.isTrue(item.getVisible())).collect(Collectors.toList());
        List<DeptUser> invisibleMembers = users.stream().filter(item ->
                Const.Bool.isFalse(item.getVisible())).collect(Collectors.toList());

        this.setMembers(members.stream().map(item ->
                new DictionaryVo(item.getUserId(), item.getUserName()))
                .collect(Collectors.toList()));
        this.setInvisibleMembers(invisibleMembers.stream().map(item ->
                new DictionaryVo(item.getUserId(), item.getUserName()))
                .collect(Collectors.toList()));
    }

    public DictionaryVo getParent() {
        return parent;
    }

    public void setParent(DictionaryVo parent) {
        this.parent = parent;
    }

    public List<DictionaryVo> getMembers() {
        return members;
    }

    public void setMembers(List<DictionaryVo> members) {
        this.members = members;
    }

    public List<DictionaryVo> getInvisibleMembers() {
        return invisibleMembers;
    }

    public void setInvisibleMembers(List<DictionaryVo> invisibleMembers) {
        this.invisibleMembers = invisibleMembers;
    }

    public DictionaryVo getAdmin() {
        return admin;
    }

    public void setAdmin(DictionaryVo admin) {
        this.admin = admin;
    }

    public List<DictionaryVo> getAdmins() {
        return admins;
    }

    public void setAdmins(List<DictionaryVo> admins) {
        this.admins = admins;
    }

}
